package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import application.Piece;


public class PieceButton extends JButton {
	
	
	private MainPanel mainPanel;
	private Piece piece;
	

	public PieceButton(MainPanel mainPanel, Piece piece) {
		this.mainPanel = mainPanel;
		this.piece = piece;
		
		this.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				mainPanel.setGuessed(piece.getKeyWord(), !piece.isGuessed());
				
			}
		});
		
		initLayout();
	}
	
	private void initLayout() {
		setHorizontalAlignment(LEFT);
		setVerticalAlignment(TOP);
		setFont(new Font("Segoe UI", Font.PLAIN, 42));
		
		if(piece.isGuessed())
			initGuessedLayout();
		else initUnguessedLayout();
	}
	
	private void initGuessedLayout() {
		setText("<html>"
				+ "<p><font color=gray>"
				+ piece.getHelpWord()
				+ "</font></p>"
				+ "<p><font color=green><i>"
				+ piece.getKeyWord()
				+ "</i></font></p>"
				+ "</html>"); 
	}
	
	private void initUnguessedLayout() {
		setText("<html>"
				+ "<p><font color=red>"
				+ piece.getHelpWord()
				+ "</font></p>"
				+ "</html>");
	}

}
